package com.rc.saas.mini.service.order;

import com.rc.saas.mini.common.level.UserLevelConstant;
import com.rc.saas.mini.mapper.order.OrderDiscountRecordMapper;
import com.rc.saas.mini.model.order.OrderDiscountRecord;
import com.rc.saas.mini.model.order.OrderDiscountRecordExample;
import com.rc.saas.mini.vo.order.OrderDiscountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/4/23
 */
@Service
public class OrderDiscountRecordServiceImpl implements OrderDiscountRecordService {

    @Autowired
    private OrderDiscountRecordMapper orderDiscountRecordMapper;

    @Override
    public OrderDiscountRecordMapper getMapper() {
        return orderDiscountRecordMapper;
    }

    @Override
    public List<OrderDiscountRecord> selectByOrderId(String tenantCode, Long orderId) {
        OrderDiscountRecordExample example = new OrderDiscountRecordExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andOrderIdEqualTo(orderId);
        return orderDiscountRecordMapper.selectByExample(example);
    }

    @Override
    public List<OrderDiscountVo> listOrderDiscountVo(Long orderId) {

        ArrayList<OrderDiscountVo> list = new ArrayList<>();

        OrderDiscountRecordExample orderDiscountRecordExample = new OrderDiscountRecordExample();
        orderDiscountRecordExample.createCriteria().andOrderIdEqualTo(orderId);
        orderDiscountRecordExample.setOrderByClause("discount_amount  desc ");

        List<OrderDiscountRecord> orderDiscounts = orderDiscountRecordMapper.selectByExample(orderDiscountRecordExample);
        if (CollectionUtils.isEmpty(orderDiscounts)) {
            return list;
        }

        for (OrderDiscountRecord orderDiscount : orderDiscounts) {
            //成长值不计算在内
            if (orderDiscount.getDiscountType().equals(UserLevelConstant.POWER_EMIRICALVALUE + "")) {
                continue;
            }
            OrderDiscountVo orderDiscountVo = new OrderDiscountVo(orderDiscount);
            list.add(orderDiscountVo);
        }
        return list;
    }
}
